import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { UserService } from 'src/app/service/user.service';
import { YearawardService } from '../../yearaward.service';
import { BaseService } from 'src/app/service/base.service';
import { UtilsService } from 'src/app/service/utils.service';
import { Router, Route } from '@angular/router';
@Component({
  selector: 'app-leveltask',
  templateUrl: './leveltask.component.html',
  styleUrls: ['./leveltask.component.scss', '../../commonList.scss'],
})
export class LeveltaskComponent implements OnInit, OnDestroy {
  @Input() timeConfig: any[] = [];
  /* ___ _ ___ */
  public deployUrl: string; /* 资源地址前缀 */
  public reshing = null;
  /* ___ _ ___ */
  public isPC = false; /* 是否pc */
  public isAndroid = false; /* 是否安卓 */
  public onRefrash: any; /* 刷新中 */
  public timeArr: any[] = [];
  public nowDate: any;
  public pageSize = 30; /* 每页条数 */
  public tabs: number[] = [1, 2, 3]; /* tabs */
  /* ___ _ ___ */
  public curTab = 3; /* 当前tab 1入围赛 2突围赛 3魅力争夺赛 */
  public top3List: EndYear.IAnchor[] = []; /* TOP3 */
  public othersList: EndYear.IAnchor[] = []; /* OTHERS */
  public site: number = 0;
  public currentAnchorInfo: EndYear.ICurrentAnchor = {
    nickname: '',
    avatarUrl: '',
    score: 0,
    rankDesc: '-',
    rank: 0,
    isMysteryMan: false,
  }; /* 当前主播信息 */

  constructor(
    private userService: UserService,
    private yearawardService: YearawardService,
    private baseService: BaseService,
    private utilsservice: UtilsService,
    private router: Router
  ) {}

  ngOnInit(): void {
    // 获取
    this.deployUrl = this.userService.getDeployUrl();
    this.isPC = this.userService.getIsPc();
    this.isAndroid = this.utilsservice.getBrowserVersion();
    this.initPage();
    // 组件刷新通信
    this.reshing = this.utilsservice.MsgSub.subscribe((res: any) => {
      this.initPage();
    });
    // 请求数据
  }
  ngOnDestroy() {
    if (this.reshing) {
      this.reshing.unsubscribe();
    }
  }
  public initPage() {
    this.timeArr = this.timeConfig
      .filter((itm) => itm.type === 7 || itm.type === 34)
      .sort(this.sortBy('startTime', undefined)); // 时间配置
    this.nowDate = new Date().getTime();
    // 活动未开始
    if (this.nowDate < this.timeArr[0].endTime * 1000) {
      this.curTab = 1;
      return;
    }
    // 已结榜
    if (this.nowDate > this.timeArr[0].endTime * 1000) {
      this.getChampionPool();
    }
  }

  // 获取冠军奖池
  public getChampionPool() {
    this.yearawardService.championPool(
      (res: any) => {
        if (res.code === 200) {
          this.site = res.data.site;
          if (res.data.site < 11) {
            this.nowDate = new Date().getTime();
            if (this.nowDate < (this.timeArr[0].endTime + 600) * 1000) {
              this.curTab = 1;
            } else {
              this.curTab = 2;
            }
          } else {
            this.curTab = 3;
          }
        }
      },
      (err: any) => {}
    );
  }

  // 排序方法 attr为排序key,rev false升序 true降序
  public sortBy(attr: any, rev: any) {
    if (rev === undefined) {
      rev = 1;
    } else {
      rev = rev ? 1 : -1;
    }

    return (a, b) => {
      a = a[attr];
      b = b[attr];
      if (a < b) {
        return rev * -1;
      }
      if (a > b) {
        return rev * 1;
      }
      return 0;
    };
  }
  /**
   * @param tab 当前tab
   * @description: 切换tab
   */
  onTabClick(tab) {
    this.curTab = tab;
  }
}
